package com.bsh.api;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bsh.api.Exception.ApiException;
import com.bsh.api.Exception.TokenException;
import com.bsh.service.ApiUserService;
import com.bsh.utils.ApiMessage;
import com.bsh.utils.IpUtil;

/**
 * 用户相关API
 * 
 * @author dl
 * @since 1.0.0
 * 
 */
@Controller
@RequestMapping(value = "/api/v1/user", method = { RequestMethod.POST })
public class ApiUserController {
	private static Logger log = LoggerFactory.getLogger(ApiUserController.class);
	@Autowired
	HttpServletRequest request;
	@Autowired
	HttpServletResponse response;
	@Autowired
	HttpSession session;
    @Autowired
    ApiUserService apiUserService;
	
	
	/**
	 * 用户注册
	 * 
	 * @param paramStr
	 * @return
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/register", method = { RequestMethod.POST })
	@ResponseBody
	public ApiMessage register(@RequestParam String paramStr) {
		ApiMessage msg = new ApiMessage();
		try {
			JSONObject dto = (JSONObject) JSON.parse(paramStr);
			dto.put("ip", IpUtil.getIpAddr(request));
			msg = apiUserService.register(dto);
		} catch (ApiException e) {
			msg.setCode(e.getCode());
			msg.setMessage(e.getMessage());
		} catch (Exception e) {
			msg = new ApiMessage("400", "请求失败");
			e.printStackTrace();
		} finally {
			return msg;
		}
	}
	/**
	 * 修改密码
	 * 
	 * @param userName
	 * @param password
	 * @param token
	 * @return
	 * @throws TokenException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/password/change", method = { RequestMethod.POST })
	@ResponseBody
	public ApiMessage changePassword(@RequestParam String paramStr) {
		ApiMessage msg = new ApiMessage();
		try {
			JSONObject dto = (JSONObject) JSON.parse(paramStr);
			msg = apiUserService.changePassword(dto);
		} catch (ApiException e) {
			msg.setCode(e.getCode());
			msg.setMessage(e.getMessage());
		} catch (Exception e) {
			msg = new ApiMessage("400", "查询失败");
			e.printStackTrace();
		} finally {
			return msg;
		}
	
	}

	/**
	 * 重置密码
	 * 
	 * @param userName
	 * @param password
	 * @param token
	 * @return
	 * @throws TokenException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/password/reset", method = { RequestMethod.POST })
	@ResponseBody
	public ApiMessage resetPassword(@RequestParam String paramStr) {
		ApiMessage msg = new ApiMessage();
		try {
			JSONObject dto = (JSONObject) JSON.parse(paramStr);
			msg = apiUserService.resetPassword(dto);
		} catch (ApiException e) {
			msg.setCode(e.getCode());
			msg.setMessage(e.getMessage());
		} catch (Exception e) {
			msg = new ApiMessage("400", "查询失败");
			e.printStackTrace();
		} finally {
			return msg;
		}
	}

	/**
	 * 申请成为商家
	 * 
	 * @param paramStr
	 * @return
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/upgrade/merchant", method = { RequestMethod.POST })
	@ResponseBody
	public ApiMessage applyMerchant(@RequestParam String paramStr) {
		ApiMessage msg = new ApiMessage();
		try {
			JSONObject dto = (JSONObject) JSON.parse(paramStr);
			msg = apiUserService.applyMerchant(dto);
		} catch (ApiException e) {
			msg.setCode(e.getCode());
			msg.setMessage(e.getMessage());
		} catch (Exception e) {
			msg = new ApiMessage("400", "请求失败");
			e.printStackTrace();
		} finally {
			return msg;
		}
	}

	/**
	 * 首页个人信息
	 * 
	 * @param paramStr
	 * @return
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/info", method = { RequestMethod.POST })
	@ResponseBody
	public ApiMessage info(@RequestParam String paramStr) {
		ApiMessage msg = new ApiMessage();
		try {
			JSONObject dto = (JSONObject) JSON.parse(paramStr);
			dto.put("ip", ApiUtils.getUrl(request));
			msg = apiUserService.info(dto);
		} catch (ApiException e) {
			msg.setCode(e.getCode());
			msg.setMessage(e.getMessage());
		} catch (Exception e) {
			msg = new ApiMessage("400", "请求失败");
			e.printStackTrace();
		} finally {
			return msg;
		}
	}

	/**
	 * 查询个人信息
	 * 
	 * @param paramStr
	 * @param headPic
	 * @return
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/getUserInfo", method = { RequestMethod.POST })
	@ResponseBody
	public ApiMessage getUserInfo(@RequestParam String paramStr) {
		ApiMessage msg = new ApiMessage();
		try {
			JSONObject dto = (JSONObject) JSON.parse(paramStr);
			dto.put("ip", ApiUtils.getUrl(request));
			msg = apiUserService.getUserInfo(dto);
		} catch (ApiException e) {
			msg.setCode(e.getCode());
			msg.setMessage(e.getMessage());
		} catch (Exception e) {
			msg = new ApiMessage("400", "请求失败");
			e.printStackTrace();
		} finally {
			return msg;
		}

	}

	/**
	 * 修改个人信息
	 * 
	 * @param paramStr
	 * @return
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/changeUserInfo", method = { RequestMethod.POST })
	@ResponseBody
	public ApiMessage changeUserInfo(
			@RequestParam String paramStr,
			@RequestParam(value = "headPic", required = false) MultipartFile headPic) {
		ApiMessage msg = new ApiMessage();
		try {
			JSONObject dto = (JSONObject) JSON.parse(paramStr);
			dto.put("ip", ApiUtils.getUrl(request));
			msg = apiUserService.changeUserInfo(dto,headPic);
		} catch (ApiException e) {
			msg.setCode(e.getCode());
			msg.setMessage(e.getMessage());
		} catch (Exception e) {
			msg = new ApiMessage("400", "请求失败");
			e.printStackTrace();
		} finally {
			return msg;
		}

	}

	/**
	 * 我的推荐商户列表
	 * 
	 * @param paramStr
	 * @return
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/getMyRecMerchantList", method = { RequestMethod.POST })
	@ResponseBody
	public ApiMessage getMyRecMerchantList(@RequestParam String paramStr) {
		ApiMessage msg = new ApiMessage();
		try {
			JSONObject dto = (JSONObject) JSON.parse(paramStr);
			dto.put("ip", ApiUtils.getUrl(request));
			msg = apiUserService.getMyRecMerchantList(dto);
		} catch (ApiException e) {
			msg.setCode(e.getCode());
			msg.setMessage(e.getMessage());
		} catch (Exception e) {
			msg = new ApiMessage("400", "请求失败");
			e.printStackTrace();
		} finally {
			return msg;
		}

	}

	/**
	 * 我的推荐会员列表
	 * 
	 * @param paramStr
	 * @return
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/getMyRecUserList", method = { RequestMethod.POST })
	@ResponseBody
	public ApiMessage getMyRecUserList(@RequestParam String paramStr) {
		ApiMessage msg = new ApiMessage();
		try {
			JSONObject dto = (JSONObject) JSON.parse(paramStr);
			dto.put("ip", ApiUtils.getUrl(request));
			msg = apiUserService.getMyRecUserList(dto);
		} catch (ApiException e) {
			msg.setCode(e.getCode());
			msg.setMessage(e.getMessage());
		} catch (Exception e) {
			msg = new ApiMessage("400", "请求失败");
			e.printStackTrace();
		} finally {
			return msg;
		}

	}
	/**
	 * 我的推荐微信列表
	 * @param paramStr
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/getWxRecList", method = { RequestMethod.POST })
	@ResponseBody
	public ApiMessage getWxRecList(@RequestParam String paramStr) throws Exception {
		ApiMessage msg = new ApiMessage();
		try {
			JSONObject dto = (JSONObject) JSON.parse(paramStr);
			msg = apiUserService.getWxRecList(dto);
		} catch (ApiException e) {
			msg.setCode(e.getCode());
			msg.setMessage(e.getMessage());
		} catch (Exception e) {
			msg = new ApiMessage("400", "请求失败");
			e.printStackTrace();
		} finally {
			return msg;
		}
	}

	

}
